﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.loading.loaders {
    import flash.events.*;
    import flash.net.*;
    import net.wooga.pets.loading.vos.*;
    import net.wooga.pets.loading.interfaces.*;
    import org.osflash.signals.*;
    import org.osflash.signals.natives.*;

    public class AbstractLoader implements IRequestLoader {

        private var _state:String = "PENDING"
        private var _id:String;
        private var _pipeId:String;
        private var _path:String;
        private var _method:String="get";
        private var _params:URLVariables;
        private var _currentSize:int;
        private var _totalSize:int;
        protected var _result:ResultVO;
        protected var _retries:int = 0
        private var _loaded:Signal;
        private var _progressing:Signal;
        private var _completeSignal:NativeSignal;
        private var _errorSignal:NativeSignal;
        private var _progressSignal:NativeSignal;

        protected static const MAX_RETRIES:int = 3;
        private static const PENDING:String = "PENDING";
        private static const LOADING:String = "LOADING";
        private static const FINISHED:String = "FINISHED";

        public function AbstractLoader(){
            this._loaded = new Signal(String, ResultVO);
            this._progressing = new Signal();
            super();
            this._result = new ResultVO();
            this._completeSignal = new NativeSignal(this.dispatcher, Event.COMPLETE, Event);
            this._completeSignal.add(this.onComplete);
            this._errorSignal = new NativeSignal(this.dispatcher, IOErrorEvent.IO_ERROR, IOErrorEvent);
            this._errorSignal.add(this.onError);
            this._progressSignal = new NativeSignal(this.dispatcher, ProgressEvent.PROGRESS, ProgressEvent);
            this._progressSignal.add(this.onProgress);
        }
        public function start():void{
            this._state = LOADING;
        }
        public function set id(_arg1:String):void{
            this._id = _arg1;
        }
        public function get id():String{
            return (this._id);
        }
        public function set pipeId(_arg1:String):void{
            this._pipeId = _arg1;
        }
        public function get pipeId():String{
            return (this._pipeId);
        }
        public function set path(_arg1:String):void{
            this._path = _arg1;
            this._result.path = this._path;
        }
        public function get path():String{
            return (this._path);
        }
        public function get method():String{
            return (this._method);
        }
        public function set method(_arg1:String):void{
            this._method = _arg1;
        }
        public function get params():URLVariables{
            return (this._params);
        }
        public function set params(_arg1:URLVariables):void{
            this._params = _arg1;
        }
        public function set currentSize(_arg1:int):void{
            this._currentSize = _arg1;
        }
        public function get currentSize():int{
            return (this._totalSize);
        }
        public function set totalSize(_arg1:int):void{
            this._totalSize = _arg1;
        }
        public function get totalSize():int{
            return (this._totalSize);
        }
        public function get isPending():Boolean{
            return ((this._state == PENDING));
        }
        public function get isLoading():Boolean{
            return ((this._state == LOADING));
        }
        public function get isFinished():Boolean{
            return ((this._state == FINISHED));
        }
        public function get result():ResultVO{
            return (this._result);
        }
        public function get progressing():Signal{
            return (this._progressing);
        }
        public function get loaded():Signal{
            return (this._loaded);
        }
        protected function get data(){
            return (null);
        }
        protected function get dispatcher():IEventDispatcher{
            return (null);
        }
        protected function getRequest(_arg1:String):URLRequest{
            var _local2:URLRequest = new URLRequest(_arg1);
			if(this.method!=null)
            	_local2.method = this.method;
			if(this.params!=null)
            	_local2.data = this.params;
            return (_local2);
        }
        private function onProgress(_arg1:ProgressEvent):void{
            this.progressing.dispatch();
        }
        protected function handleLoadedData():void{
            this._result.success = true;
            this._result.data = this.data;
        }
        protected function onError(_arg1:IOErrorEvent):void{
            this.finish();
        }
        private function onComplete(_arg1:Event):void{
            this.handleLoadedData();
            this.finish();
        }
        private function finish():void{
            this._state = FINISHED;
            this.loaded.dispatch(this._id, this._result);
            this._progressing.removeAll();
            this._progressSignal.removeAll();
            this._completeSignal.removeAll();
            this._errorSignal.removeAll();
        }

    }
}//package net.wooga.pets.loading.loaders 
